package com.adee.gulimall.product.app;

import com.adee.gulimall.common.utils.PageUtils;
import com.adee.gulimall.util.utils.R;
import com.adee.gulimall.product.entity.ProductAttrValueEntity;
import com.adee.gulimall.product.service.AttrGroupService;
import com.adee.gulimall.product.service.AttrService;
import com.adee.gulimall.product.vo.AttrRespVo;
import com.adee.gulimall.product.vo.AttrVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;



/**
 * 商品属性
 *
 * @author adee
 * @email 609980188@qq.com
 * @date 2022-08-19 16:28:50
 */
@RestController
@RequestMapping("product/attr")
public class AttrController {
    @Autowired
    private AttrService attrService;

    @Autowired
    private AttrGroupService attrGroupService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    // @RequiresPermissions("product:attr:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = attrService.queryPage(params);

        return R.ok().put("page", page);
    }

    // attrType：base-规格参数，sale-销售属性
    @RequestMapping("/{attrType}/list/{catelogId}")
    // @RequiresPermissions("product:attr:list")
    public R baseList(@RequestParam Map<String, Object> params,
                      @PathVariable Long catelogId,
                      @PathVariable String attrType){
        PageUtils page = attrService.queryPage(params, catelogId, attrType);

        return R.ok().put("page", page);
    }

    // 根据商品id查询商品基本属性
    @RequestMapping("/base/listforspu/{spuId}")
    // @RequiresPermissions("product:attr:list")
    public R baseList4Spu(@PathVariable Long spuId){
        List<ProductAttrValueEntity> list = attrService.queryBaseList4Spu(spuId);

        return R.ok().put("data", list);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrId}")
    // @RequiresPermissions("product:attr:info")
    public R info(@PathVariable("attrId") Long attrId){
		// AttrEntity attr = attrService.getById(attrId);
        AttrRespVo vo = attrService.getAttrInfo(attrId);

        return R.ok().put("attr", vo);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    // @RequiresPermissions("product:attr:save")
    public R save(@RequestBody AttrVo attr){
		attrService.saveAttr(attr);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    // @RequiresPermissions("product:attr:update")
    public R update(@RequestBody AttrVo attr){
        // attrService.updateById(attr);
        attrService.updateAttr(attr);

        return R.ok();
    }

    /**
     * 修改
     */
    @PostMapping("/update/{spuId}")
    // @RequiresPermissions("product:attr:update")
    public R updateBySpuId(@RequestBody List<ProductAttrValueEntity> attrs, @PathVariable("spuId") Long spuId){
        attrService.updateAttrsBySpuId(attrs, spuId);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    // @RequiresPermissions("product:attr:delete")
    public R delete(@RequestBody Long[] attrIds){
		attrService.removeByIds(Arrays.asList(attrIds));

        return R.ok();
    }

}
